package com.goods.business.service.imp;

import com.goods.business.mapper.HealthMapper;
import com.goods.business.service.HealthService;
import com.goods.common.model.business.Health;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.HealthVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class HealthServiceImpl implements HealthService {

    @Resource
    private HealthMapper healthMapper;

    @Override
    public HealthVO isReport(Long id) {
        Date date = new Date();
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Health health = healthMapper.isReport(id, date);
        HealthVO healthVO = new HealthVO();
        if (health == null){
            return null;
        }
        BeanUtils.copyProperties(health,healthVO);
        return healthVO;

    }

    @Override
    public void report(HealthVO healthVO,Long userId) {
        Health health = new Health();
        if (healthVO!= null){
            BeanUtils.copyProperties(healthVO,health);
            health.setUserId(userId);
            health.setCreateTime(new Date());
            healthMapper.insert(health);
        }
    }

    @Override
    public Map history(Integer pageNum, Integer pageSize, Long userId) {
        List<Health> healthList = healthMapper.selectByUserId(userId);

        List<HealthVO> healthVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(healthList)){
            for (Health health : healthList) {
                HealthVO healthVO = new HealthVO();
                BeanUtils.copyProperties(health,healthVO);
                healthVOList.add(healthVO);
            }
        }

        List<HealthVO> page = ListPageUtils.page(healthVOList, pageSize, pageNum);
        int total = healthVOList.size();
        Map map = new HashMap();
        map.put("rows",page);
        map.put("total",total);
        return map;
    }
}
